package com.starblink.android.basic.user

import android.content.Context
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ToastUtils
import com.starblink.android.basic.ext.gotoActivityForResult

object LoginCheckInterceptor {

    var classLogin: Class<*>? = null

    fun injectLoginClazz(classLogin: Class<*>) {
        LoginCheckInterceptor.classLogin = classLogin
    }

    inline fun <reified T> checkLogin(
        ctx: FragmentActivity,
        crossinline goAheadAndDoItMethod: () -> Unit
    ) {
        if (UserDataCenter.isLogin()) {
            goAheadAndDoItMethod()
        } else {
            ctx.gotoActivityForResult<T> {
                val lg = it?.getBooleanExtra("isLogin", false)
                if (lg == true) {
                    goAheadAndDoItMethod()
                }
            }
        }
    }

    /**
     * 无论登录成功与否都需要回调 ifMustCallBack=true
     */
    inline fun check2Login(
        ctx: Context,
        ifMustCallBack:Boolean=false,
        crossinline goAheadAndDoItMethod: () -> Unit,
    ) {
        if (classLogin == null) {
            ToastUtils.showLong("your application must implement ILoginApp")
            return
        }
        if (UserDataCenter.isLogin()) {
            goAheadAndDoItMethod()
        } else {
            ctx.gotoActivityForResult(classLogin!!) {
                val lg = it?.getBooleanExtra("isLogin", false)
                if (lg == true || ifMustCallBack) {
                    goAheadAndDoItMethod()
                }
            }
        }
    }

}